import { initFullLayer } from '../../utils/LayerUtil';
import { projectWebMercatorToLngLat } from '../../../../mapUtils/CoordinateUtil';
import ITool from '../ITool';

/**
 * 高亮工具箱
 * @implements {ITool}
 */
class HighlightTool extends ITool {
  constructor(_top, _impl) {
    super(_top, _impl);
    this.name = 'HighlightTool';
    this.desc = '高亮工具箱';
    this.layeridPrefix = 'HighlightTool';
    // 是否闪烁图层
    this.useFlash = false;
    // 闪烁次数
    this.flashCount = 10;
    // 闪烁间隔:毫秒
    this.flashInterval = 300;
  }

  onAdd() {
    super.onAdd();
    initFullLayer(this);
  }

  onRemove() {
    super.onRemove();
  }

  /**
   * 高亮元素
   * @param {features} features 图形数据
   * @param {Boolean} useFlash 是否闪烁
   */
  highlight(features, useFlash) {
    this.moveLayerToTop();
    features.forEach((feature) => {
      feature.geometry.coordinates = projectWebMercatorToLngLat(feature.geometry.coordinates);
    });
    this.stopFlash();
    this.getMap().getSource(this._sourceId).setData(turf.featureCollection(features));
    let flash = useFlash !== undefined ? useFlash : this.useFlash;
    if (flash) {
      this.startFlash();
    }
  }

  /**
   * 移除高亮元素
   */
  unhighlight() {
    this.stopFlash();
    this.getMap().getSource(this._sourceId).setData(turf.featureCollection([]));
  }

  /**
   * 将高亮图层移到地图最上层
   * @ignore
   */
  moveLayerToTop() {
    this._layerIdsAll.forEach((layerid) => {
      this.getMap().moveLayer(layerid);
    });
  }

  /**
   * 启动闪烁
   */
  startFlash() {
    // 重置
    this.stopFlash();
    var count = 0;
    this.flashIntervalInstance = setInterval(() => {
      // 透明度变换规律1-0.5-1-0.5-1....
      var opacity = [0.5, 1][count % 2];
      this._changeOpacity(opacity);
      count++;
      // 到达指定闪烁次数时，重置
      if (count > this.flashCount) {
        this.stopFlash();
      }
    }, this.flashInterval);
  }

  /**
   * 停止闪烁
   */
  stopFlash() {
    // 移除定时器
    if (this.flashIntervalInstance) {
      clearInterval(this.flashIntervalInstance);
      this.flashIntervalInstance = null;
    }
    // 重置图层透明度
    this.getMap().setPaintProperty(this._layerMarkerId, 'icon-opacity', ['get', 'opacity']);
    this.getMap().setPaintProperty(this._layerMarkerId, 'text-opacity', ['get', 'opacity']);
    this.getMap().setPaintProperty(this._layerPointId, 'circle-opacity', ['get', 'opacity']);
    this.getMap().setPaintProperty(this._layerLinestringId, 'line-opacity', ['get', 'opacity']);
    this.getMap().setPaintProperty(this._layerPolygonId, 'fill-opacity', ['get', 'opacity']);
  }

  /**
   * 设置闪烁参数
   * @param {Object} option 配置参数
   */
  setFlashOption(option) {
    if (option.useFlash) {
      this.useFlash = option.useFlash;
    }
    if (option.flashCount) {
      this.flashCount = option.flashCount;
    }
    if (option.flashInterval) {
      this.flashInterval = option.flashInterval;
    }
  }

  /**
   * 更改高亮图层透明度
   * @param {Number} opacity 透明度
   * @ignore
   */
  _changeOpacity(opacity) {
    this.getMap().setPaintProperty(this._layerMarkerId, 'icon-opacity', opacity);
    this.getMap().setPaintProperty(this._layerMarkerId, 'text-opacity', opacity);
    this.getMap().setPaintProperty(this._layerPointId, 'circle-opacity', opacity);
    this.getMap().setPaintProperty(this._layerLinestringId, 'line-opacity', opacity);
    this.getMap().setPaintProperty(this._layerPolygonId, 'fill-opacity', opacity);
  }
}

export default HighlightTool;
